/**
 * Created by Noir on 1/27/14.
 */
var User = require("../models/user"),
    helper = require("../../helper/helper");

exports.follow = function(req, res, next){
    // add user who is on session to follow list of owner
    helper.getEmbeddedUserById(req.body.id, function(err, owner){
        if(err){
            console.log(err);
            return res.send(400, 'Something wrong just happened. Please try again.');
        }

        if(owner){
            helper.getEmbeddedUserById(req.session.passport.user.id,function(err, onliner){
                if(err){
                    console.log(err);
                    return res.send(400, 'Something wrong just happened. Please try again.');
                }

                if(onliner){
                    // if both onliner id and owner id exist
                    // add onliner id to owner's followed list
                    User.update({'_id':owner.userId},{$push:{followed:{'userId':onliner.userId,'avatar':onliner.avatar,'username':onliner.username}}}, function(err, user){
                        if(err){
                            console.log(err);
                            return res.send(400, 'Something wrong just happened. Please try again.');
                        }

                        // add owner id to onliner's follow list
                        User.update({'_id':onliner.userId},{$push:{follow:{'userId':owner.userId,'avatar':owner.avatar,'username':owner.username}}},function(err){
                            if(err){
                                console.log(err);
                                return res.send(400, 'Something wrong just happened. Please try again.');
                            }

                            return res.send('followed');
                        });
                    });
                }else{
                    // ask user to sign in
                    return res.send(400);
                }
            })
        }else{
            // ask user to sign in
            return res.send(400);
        }
    });
}

exports.unfollow = function(req, res, next){
    // add user who is on session to follow list of owner
    helper.getEmbeddedUserById(req.body.id, function(err, owner){
        if(err){
            console.log(err);
            return res.send(400, 'Something wrong just happened. Please try again.');
        }

        if(owner){
            helper.getEmbeddedUserById(req.session.passport.user.id,function(err, onliner){
                if(err){
                    console.log(err);
                    return res.send(400, 'Something wrong just happened. Please try again.');
                }

                if(onliner){
                    // if both onliner id and owner id exist
                    // add onliner id to owner's followed list
                    User.update({'_id':owner.userId},{$pull:{followed:{'userId':onliner.userId,'avatar':onliner.avatar,'username':onliner.username}}}, function(err, user){
                        if(err){
                            console.log(err);
                            return res.send(400, 'Something wrong just happened. Please try again.');
                        }

                        // add owner id to onliner's follow list
                        User.update({'_id':onliner.userId},{$pull:{follow:{'userId':owner.userId,'avatar':owner.avatar,'username':owner.username}}},function(err){
                            if(err){
                                console.log(err);
                                return res.send(400, 'Something wrong just happened. Please try again.');
                            }

                            return res.send('unfollow');
                        });
                    });
                }else{
                    // ask user to sign in
                    return res.send(400);
                }
            })
        }else{
            // ask user to sign in
            return res.send(400);
        }
    });
}